package com.xsp.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xsp.commonutils.JwtUtils;
import com.xsp.commonutils.MD5;
import com.xsp.commonutils.servicebase.exceptionhandler.GuLiException;
import com.xsp.ucenter.entity.Member;
import com.xsp.ucenter.entity.vo.LoginVo;
import com.xsp.ucenter.entity.vo.RegisterVo;
import com.xsp.ucenter.mapper.MemberMapper;
import com.xsp.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author xsp
 * @since 2022-02-21
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(LoginVo loginVo) {
        //验证账号密码是否为空
        if (StringUtils.isEmpty(loginVo.getMobile()) || StringUtils.isEmpty(loginVo.getPassword())){
            throw new GuLiException(20001,"账号密码不能为空！");
        }
        //找到对应手机号的数据
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile",loginVo.getMobile());
        Member member = baseMapper.selectOne(memberQueryWrapper);
        //验证数据是否存在
        if (member == null){
            throw new GuLiException(20001,"该手机号不存在！");
        }
        //判断和数据库的密码是否相同，数据库中存储的密码是md5加密的，因此需要先对传过来的密码进行加密
        if (!MD5.encrypt(loginVo.getPassword()).equals(member.getPassword())){
            throw new GuLiException(20001,"密码错误！");
        }
        //isdisabled字段如果是true说明该用户被禁用，不能登录
        if (member.getIsDisabled()){
            throw new GuLiException(20001,"该账号已被禁用！");
        }
        //通过jwt工具传入 数据库的id和用户名 返回token
        String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getNickname());

        return jwtToken;
    }

    //注册
    @Override
    public void register(RegisterVo registerVo) {
        //拿到数据
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        //非空校验
        if (StringUtils.isEmpty(nickname) || StringUtils.isEmpty(mobile)
        || StringUtils.isEmpty(password) || StringUtils.isEmpty(code)){
            throw new GuLiException(20001,"不能为空！");
        }
        //从redis中获取该手机号的验证码进行校验
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new GuLiException(20001,"验证码错误!");
        }
        //如果能拿到数据，说明改手机号存在
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile",mobile);
        Integer mobileCount = baseMapper.selectCount(memberQueryWrapper);
        if (mobileCount > 0){
            throw new GuLiException(20001,"该手机号已存在！");
        }

        //最终保存数据
        Member member = new Member();
        member.setNickname(nickname);//昵称
        member.setMobile(mobile);//手机号
        member.setPassword(MD5.encrypt(password));//密码加密后保存
        member.setIsDisabled(false);//设置禁用为false
        member.setAvatar("https://shepi-1010.oss-cn-shenzhen.aliyuncs.com/a95790f5ly1giblxo6r3bj215o0xc7wh.jpg");//默认头像
        baseMapper.insert(member);//插入
    }

    //登录后获取信息
    @Override
    public Member getLoginInfo(String memberId) {
        Member member = baseMapper.selectById(memberId);
        return member;
    }

    //通过openid获取信息
    @Override
    public Member getByOpenid(String openid) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid",openid);
        Member member = baseMapper.selectOne(queryWrapper);
        return member;
    }

    //统计某一天的注册人数
    @Override
    public Integer registerCountByDay(String day) {
        Integer count = baseMapper.registerCountByDay(day);
        return count;
    }
}
